package cn.itsource.service.system.imp;




import cn.itsource.mapper.system.RoleMapper;
import cn.itsource.service.system.IRoleService;
import cn.itsource.system.domain.Role;
import cn.itsource.system.query.RoleQuery;
import cn.itsource.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;



    @Override
    public List<Role> selectAll() {
        try {

            JsonResult.success("查询用户数据成功！");
            return roleMapper.selectAll();

        } catch (Exception e) {
            e.printStackTrace();
            JsonResult.error();
            return null;
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            roleMapper.delete(id);
            // where id = #{id} AND name IS NULL


            JsonResult.success("删除用户数据成功！");

        } catch (Exception e) {
            e.printStackTrace();
            JsonResult.error();
        }

    }

    @Override
    public void update(Role role) {
        if (role.getId() == null) {
            roleMapper.add(role);

            System.out.println("新增");
            JsonResult.success("新增用户数据成功！");
        } else {
            roleMapper.update(role);


            System.out.println("修改");
            JsonResult.success("修改用户数据成功！");
        }

    }

    @Override
    public void deleteMore(Long[] ids) {
        roleMapper.deleteMore(ids);
    }



    @Override
    public PageInfo<Role> queryRole(RoleQuery roleQuery) {
        // 设置分页参数
        PageHelper.startPage(roleQuery.getCurrentPage(), roleQuery.getPageSize());


        List<Role> roles = roleMapper.queryRole(roleQuery);
        System.out.println(roles);
        return new PageInfo<>(roles);
    }






   /* @Override
    public List<RoleType> tree(){
        //1.获取所有RoleType
        List<Role> roleTypes = roleMapper.selectAll();
        HashMap<Integer, Role> typeHashMap = new HashMap<>();
        //2.将RoleType添加到map集合中,id作为键,RoleType作为值,方便查询
        for (Role role : roles) {
            typeHashMap.put(role.getId(),role);
        }
        //3.定义一个List<RoleType>用与存放无限级树
        List<RoleType> typeTree = new ArrayList<>();
        for (Role role : roles) {
            //4.获取父类型id,并且进行判断,如果parentId==null就说明是顶层,直接加到typeTree中
            Integer parentId = role.getParentId();
            if (parentId==null){
                //5.说明自己是顶级层,将自己添加到typeTree中
                typeTree.add(roleType);
            }else {
            *//*
                如果parentId!=null,说明有parentId
                  1.先根据parentId查询map集合中的父对象
                  2.然后判断,如果父对象!=null
                  3.获取父对象的children,将自己加入到children中
                  4.最后返回typeTree
             *//*
                RoleType parentRoleType = typeHashMap.get(parentId);
                //防止父类型名称为空时,出现空指针异常
                if (parentRoleType !=null){
                    List<RoleType> children = parentRoleType.getChildren();
                    children.add(roleType);
                }
            }
        }
        return typeTree;*/






}

